﻿using System.Linq;
using System.Security.Claims;

namespace Batman.Authentication.Middlewares
{
    public static class UserIdentityExtension
    {
        public static string GetData(this ClaimsPrincipal user, string key)
        {
            return user.Claims.Where(c => c.Type == key).Select(x => x.Value).FirstOrDefault();
        }

        /// <summary>
        /// 获取用户ID
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static string GetUserId(this ClaimsPrincipal user)
        {
            return user.Claims.Where(c => c.Type == "userId").Select(x => x.Value).FirstOrDefault();
        }

        /// <summary>
        /// 获取角色ID
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static string GetRoleId(this ClaimsPrincipal user)
        {
            return user.Claims.Where(c => c.Type == "roleId").Select(x => x.Value).FirstOrDefault();
        }
    }
}
